package com.test.fundible.fundible_00_customer_service;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.test.fundible.R;
import com.test.fundible.fundible_00_utils.FundibleUtil;
import com.test.fundible.fundible_01_launcher.App_fundibleShow_fundibleCustomize;
import com.test.fundible.fundible_00_http.OkHttpUtil;
import com.test.fundible.fundible_01_launcher.Base_fundibleApplication;

import java.util.Random;


public class Customer_fundibleServiceDialog extends Dialog {

    private LinearLayout groupLayout;

    private Runnable runnableOnDismoss;

    public Customer_fundibleServiceDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_fundible_customer_service);

        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        getWindow().setBackgroundDrawableResource(R.color.transparent);
        setCanceledOnTouchOutside(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        groupLayout = findViewById(R.id.dfcs_layout_group);

        firstRun();
    }

    @SuppressLint("SetTextI18n")
    private void firstRun(){

        findViewById(R.id.dfcs_ib_close).setOnClickListener(v -> dismiss());

        String[] mobiles = App_fundibleShow_fundibleCustomize.getInstance_fundible().getMobiles_fundible();
        String[] emails = App_fundibleShow_fundibleCustomize.getInstance_fundible().getEmails_fundible();

        for (int i=0;i<mobiles.length;i++){
            String mobile = mobiles[i];
            addCsMobile(mobile, (mobiles.length<=1 ? -1:i+1));
        }

        for (int i=0;i<emails.length;i++){
            String email = emails[i];
            addfundibleEmail(email, (emails.length<=1 ? -1:i+1));
        }

    }

    @SuppressLint("SetTextI18n")
    private void addCsMobile(String mobile, int num){
        if (TextUtils.isEmpty(mobile)) return;
        View mobileItem = View.inflate(getContext(),R.layout.item_fundible_customer_service,null);

        TextView titleTtv = mobileItem.findViewById(R.id.ifcs_tv_title);
        TextView contentTextview = mobileItem.findViewById(R.id.ifcs_tv_content);
        TextView btn = mobileItem.findViewById(R.id.ifcs_btn);

        btn.setText(Base_fundibleApplication.getInstant().getString(R.string.cs_dial));

        titleTtv.setText(getContext().getString(R.string.cs_mobile) + (num>0 ? " "+num:""));
        contentTextview.setText(mobile);

        btn.setOnClickListener(v -> {
            OkHttpUtil.getInstance_fundible().uploadEventPoint("DIALOG_CUSTOMER_SERVICE","PHONE_CALL","");
            Intent dialIntent =  new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + mobile.trim()));//跳转到拨号界面，同时传递电话号码
            getContext().startActivity(dialIntent);
        });

        groupLayout.addView(mobileItem);
    }

    @SuppressLint("SetTextI18n")
    private void addfundibleEmail(String email, int num){
        if (TextUtils.isEmpty(email)) return;

        View emailItem = View.inflate(getContext(),R.layout.item_fundible_customer_service,null);

        TextView titleTv = emailItem.findViewById(R.id.ifcs_tv_title);
        TextView contentTv = emailItem.findViewById(R.id.ifcs_tv_content);
        TextView btn = emailItem.findViewById(R.id.ifcs_btn);

        btn.setText(Base_fundibleApplication.getInstant().getString(R.string.cs_copy));

        titleTv.setText(getContext().getString(R.string.cs_email) + (num>0 ? " "+num:""));
        contentTv.setText(email);

        btn.setOnClickListener(v -> {
            OkHttpUtil.getInstance_fundible().uploadEventPoint("DIALOG_CUSTOMER_SERVICE","EMAIL_COPY","");
            ((ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE)).setPrimaryClip(ClipData.newPlainText("Label", email.trim()));
            Toast.makeText(getContext(), getContext().getString(R.string.tips_copy_success), Toast.LENGTH_SHORT).show();
        });

        groupLayout.addView(emailItem);
    }

    @Override
    public void show() {
        show(null);
        run(() -> OkHttpUtil.getInstance_fundible().uploadEventPoint("DIALOG_CUSTOMER_SERVICE","START",""));
    }

    public void show(Runnable dismissRunnable) {
        super.show();
        this.runnableOnDismoss = dismissRunnable;
        OkHttpUtil.getInstance_fundible().uploadEventPoint("DIALOG_CUSTOMER_SERVICE","START","");
    }

    @Override
    public void dismiss() {
        super.dismiss();
        OkHttpUtil.getInstance_fundible().uploadEventPoint("DIALOG_CUSTOMER_SERVICE","END","");
        if (runnableOnDismoss !=null) runnableOnDismoss.run();
    }

    private void run(Runnable runnable){
        int x = new Random().nextInt();
        if (x>0) ++x;
        else x=1;
        FundibleUtil.fundibleRun(runnable,x);
    }

}
